package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.NursingItemDTO;
import com.api.shopapi.entity.dto.NursingLevelItemRelDTO;
import com.api.shopapi.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 护理项目
 */

@RequestMapping("NursingItem")
@RestController
public class NursingItemController extends BaseController {

    @Autowired
    @Qualifier("NursingItemServiceImplOne")
    private NursingItemService nursingItemService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    @Autowired
    @Qualifier("NursingTypeServiceImplOne")
    private NursingTypeService nursingTypeService;

    @Autowired
    @Qualifier("NursingLevelServiceImplOne")
    private NursingLevelService nursingLevelService;


    @Autowired
    @Qualifier("NursingLevelItemRelServiceImplOne")
    private NursingLevelItemRelService nursingLevelItemRelService;

    @Autowired
    @Qualifier("UserServiceImplOne")
    private UserService userService;


    @Autowired
    @Qualifier("InstitutionServiceImplOne")
    private InstitutionService institutionService;


    @Autowired
    @Qualifier("InstitutionStaffRelServiceImplOne")
    private InstitutionStaffRelService institutionStaffRelService;



    @Autowired
    @Qualifier("CheckinServiceImplOne")
    private CheckinService checkinService;

    @Autowired
    @Qualifier("CheckinSetmealServiceImplOne")
    private CheckinSetmealService checkinSetmealService;



    //根据入住套餐查询护理项目
    @RequestMapping(value = "ListByCheckin", method = RequestMethod.POST)
    public MyResult ListByCheckin(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            String userCode = jsonObject.getString("userCode");
            String elderlyCode = jsonObject.getString("elderlyCode");

            PreCheck preCheck =checkSignFrontInterface(timeStr,sign,"/NursingItem/ListByCheckin");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }

            User user = userService.getByUserCode(userCode);
            Checkin checkin = checkinService.getByElderlyCodeAndInstitutionCode(elderlyCode,user.getInstitutionCode());
            CheckinSetmeal checkinSetmeal = checkinSetmealService.getByCheckinSetmealCode(checkin.getCheckinSetmealCode());
            NursingLevel nursingLevel = nursingLevelService.getByNursingLevelCode(checkinSetmeal.getNursingLevelCode());

            List<NursingLevelItemRel> listRel = nursingLevelItemRelService.getByNursingLevelCode(nursingLevel.getNursingLevelCode());

            List<NursingLevelItemRelDTO> listRelDTO = new ArrayList<>();
            for(NursingLevelItemRel nursingLevelItemRel:listRel){
                NursingLevelItemRelDTO nursingLevelItemRelDTO = mapper.map(nursingLevelItemRel,NursingLevelItemRelDTO.class);

                NursingItem nursingItem = nursingItemService.getByNursingItemCode(nursingLevelItemRel.getNursingItemCode());
                nursingLevelItemRelDTO.setNursingItemName(nursingItem.getName());
                listRelDTO.add(nursingLevelItemRelDTO);

            }

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("list",listRelDTO);

            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/NursingItem/ListByCheckin", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //列表
    @RequestMapping(value = "Page", method = RequestMethod.POST)
    public MyResult page(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            String name = jsonObject.getString("name");
            String nursingTypeCode = jsonObject.getString("nursingTypeCode");
            int status = jsonObject.getInteger("status");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck =checkSignAdmInterface(timeStr,sign,staffCode,"/NursingItem/Page");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }
            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            PageInfo<NursingItemDTO> pageInfoNursingItem = nursingItemService.getPageList(pageIndex, pageSize, institutionCode,name,status,nursingTypeCode);
//            for (NursingItem nursingItem:pageInfoNursingItem.getList()){
//                NursingType nursingType = nursingTypeService.getByNursingTypeCode(nursingItem.getNursingTypeCode());
//                nursingItem.setNursingTypeName(nursingType.getName());
//            }
            return success(pageInfoNursingItem);

        } catch (Exception e) {
            insertError("/NursingItem/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //列表
    @RequestMapping(value = "GovPage", method = RequestMethod.POST)
    public MyResult GovPage(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String name = jsonObject.getString("name");
            String nursingTypeCode = jsonObject.getString("nursingTypeCode");
            int status = jsonObject.getInteger("status");

            int pageIndex = jsonObject.getInteger("pageIndex");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck =checkSignAdmInterface(timeStr,sign,staffCode,"/NursingItem/Page");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            PageInfo<NursingItem> pageInfo = nursingItemService.pageByGovCode(pageIndex, pageSize, staff.getGovernmentCode());

            List<NursingItemDTO> listDTO = new ArrayList<>();
            for (NursingItem nursingItem:pageInfo.getList()){

                NursingItemDTO nursingItemDTO = mapper.map(nursingItem,NursingItemDTO.class);
                Institution institution = institutionService.getByInstitutionCode(nursingItem.getInstitutionCode());
                User user = userService.getByUserCode(institution.getUserCode());
                nursingItemDTO.setInstitutionName(user.getName());
                listDTO.add(nursingItemDTO);

            }

            PageInfo<NursingItemDTO> pageInfo1 = new PageInfo<>();
            pageInfo1.setList(listDTO);
            pageInfo1.setTotal(pageInfo.getTotal());

            return success(pageInfo1);

        } catch (Exception e) {
            insertError("/NursingItem/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //查询全部
    @RequestMapping(value = "List", method = RequestMethod.POST)
    public MyResult list(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck =checkSignAdmInterface(timeStr,sign,staffCode,"/NursingItem/List");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }
            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();
            List<NursingItem> nursingItemList = nursingItemService.getList(institutionCode);
            return success(nursingItemList);
        } catch (Exception e) {
            insertError("/NursingItem/List", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //添加
    @RequestMapping(value = "Add", method = RequestMethod.POST)
    public MyResult add(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String name = jsonObject.getString("name");
            String nursingTypeCode = jsonObject.getString("nursingTypeCode");
            int sort = jsonObject.getInteger("sort");
            int duration = jsonObject.getInteger("duration");

            PreCheck preCheck =checkSignAdmInterface(timeStr,sign,staffCode,"/NursingItem/Add");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            NursingItem nursingItem = nursingItemService.getByNameAndInstitutionCode(name,institutionCode);
            if (nursingItem != null) {
                return failed("该护理项目已存在");
            }

            nursingItem = new NursingItem();
            nursingItem.setNursingItemCode(UUID.randomUUID().toString());
            nursingItem.setName(name);
            nursingItem.setNursingTypeCode(nursingTypeCode);
            nursingItem.setSort(sort);
            nursingItem.setDuration(duration);
            nursingItem.setInstitutionCode(institutionCode);
            nursingItemService.insert(nursingItem);

            Object object = new Object[]{};
            return success(object);

        } catch (Exception e) {
            insertError("/NursingItem/Add", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //编辑
    @RequestMapping(value = "Edit", method = RequestMethod.POST)
    public MyResult Edit(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String nursingItemCode = jsonObject.getString("nursingItemCode");
            String name = jsonObject.getString("name");
            Integer sort = jsonObject.getInteger("sort");
            Integer status = jsonObject.getInteger("status");
            String nursingTypeCode = jsonObject.getString("nursingTypeCode");
            Integer duration = jsonObject.getInteger("duration");

            PreCheck preCheck =checkSignAdmInterface(timeStr,sign,staffCode,"/NursingItem/Edit");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            NursingItem nursingItem = nursingItemService.getByNameAndInstitutionCode(name,institutionCode);
            if (nursingItem != null) {
                if(!nursingItem.getNursingItemCode().equals(nursingItemCode)){
                    return failed("该护理等级已存在");
                }
            }

            nursingItem = nursingItemService.getByNursingItemCode(nursingItemCode);
            nursingItem.setName(name);
            nursingItem.setNursingTypeCode(nursingTypeCode);
            nursingItem.setSort(sort);
            nursingItem.setStatus(status);
            nursingItem.setDuration(duration);
            nursingItemService.update(nursingItem);

            Object object = new Object[]{};
            return success(object);
        } catch (Exception e) {
            insertError("/NursingItem/Edit", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //根据护理类型查询
    @RequestMapping(value = "ListByNursingType", method = RequestMethod.POST)
    public MyResult ListByNursingType(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String institutionCode = jsonObject.getString("institutionCode");
            String nursingTypeCode = jsonObject.getString("nursingTypeCode");
            PreCheck preCheck =checkSignFrontInterface(timeStr,sign,"/NursingItem/ListByNursingType");
            if(preCheck.getError()==1){
                return failed(preCheck.getMessage());
            }



            List<NursingItem> nursingItemList = nursingItemService.getListByNursingType(institutionCode,nursingTypeCode);
            return success(nursingItemList);
        } catch (Exception e) {
            insertError("/NursingItem/ListByNursingType", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }



}
